package com.linkoog.devtools.intellij.ui.table.row_data;

import com.intellij.util.ui.ListTableModel;
import com.linkoog.devtools.intellij.ui.table.ListTableView;
import com.linkoog.devtools.intellij.ui.table.TableRowData;
import com.linkoog.devtools.intellij.ui.table.column_info.column.StringColumnInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
public class KeyValTableRowData extends TableRowData {
    private String key;
    private String value;

    public KeyValTableRowData() {
    }

    public KeyValTableRowData(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public static ListTableView<KeyValTableRowData> createListTableView() {
        ListTableView.TablePopupMenuProvider<KeyValTableRowData> popupMenuProvider = (table, rowData, cell, row, column) -> {
            return null;
        };

        ListTableView<KeyValTableRowData> listTableView = new ListTableView<>(popupMenuProvider, null);
        StringColumnInfo<KeyValTableRowData> columnInfo1 = new StringColumnInfo<>("Name", "key", null, null);
        StringColumnInfo<KeyValTableRowData> columnInfo2 = new StringColumnInfo<>("Value", "value", null, null);

        ListTableModel<KeyValTableRowData> listTableModel = new ListTableModel<>(columnInfo1, columnInfo2);
        listTableView.setModelAndUpdateColumns(listTableModel);

        return listTableView;

    }

    public static Map<String, String> getKeyValue(ListTableView<KeyValTableRowData> listTableView) {
        List<KeyValTableRowData> items = listTableView.getListTableModel().getItems();
        Map<String, String> result = new LinkedHashMap<>();
        items.forEach(item -> result.put(item.getKey(), item.getValue()));
        return result;
    }
}
